package com.monas.client;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.splash.AbstractSplashHandler;

public class MonasSplashScreen extends AbstractSplashHandler {

	private ProgressBar fBar;

	private static final int selection = 25;
	@Override
	public void init(Shell splash) {
		super.init(splash);

		splash.setBackgroundMode(SWT.INHERIT_DEFAULT);

		Composite container = new Composite(splash, SWT.NONE);
		container.setLayout(new GridLayout(1, false));
		container.setLocation(0, 480);
		container.setSize(620, 60);

		// Progress Bar
		fBar = new ProgressBar(container, SWT.HORIZONTAL);
		fBar.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
		((GridData) fBar.getLayoutData()).heightHint = 12;
		fBar.setMaximum(100);
		fBar.setSelection(2 * selection);

		splash.layout();
	}

	@Override
	public IProgressMonitor getBundleProgressMonitor() {
		return new NullProgressMonitor() {

			@Override
			public void beginTask(String name, final int totalWork) {
				getSplash().getDisplay().syncExec(new Runnable() {
					public void run() {
						fBar.setSelection(3 * selection);
					}
				});
			}

			@Override
			public void subTask(String name) {
				getSplash().getDisplay().syncExec(new Runnable() {
					public void run() {
						if (fBar.getSelection() < 100)
							fBar.setSelection(fBar.getSelection() + 8);
					}
				});
			}

		};
	}

}
